WGC = new function(){

	var db;
	
	var userId = H5.Storage.get("userId");
	var gameId = H5.Storage.get("gameId");
	var tilesetId = H5.Storage.get("tilesetId");
	
	this.data = {};

	this.init = function(){
		WGC.data.game = {
			gameId: 0,
			name: "Test Game",
			userId: 0,
			tilesetId: 0,
			foregroundLayerId: 0,
			centerLayerId: 1,
			backgroundLayerId: 2,
			backgroundImage: "",
		};
		WGC.data.user = {
			userId: 0,
			name: "Testuser",
		};
		WGC.data.tileset = {
			tilesetId: 0,
			name: "Test tileset",
			userId: 0,
		};
		WGC.data.box = [];
		WGC.data.gameboxstate = [];
		WGC.data.gameentity = [];
		WGC.data.sprite = [];
		WGC.data.entity = [];
		WGC.data.entitystate = [];
		WGC.data.gamebox = [];
		WGC.data.gameboxlayer = [
			{
				gameBoxLayerId: 0,
				gameId: 0,
			},
			{
				gameBoxLayerId: 1,
				gameId: 0,
			},
			{
				gameBoxLayerId: 2,
				gameId: 0,
			},
		];
		
	};
	
	this.insertSprite = function(sprite){
		WGC.data.sprite.push(sprite);
		var pos = WGC.data.sprite.length -1;
		WGC.data.sprite[pos].spriteId = pos;
		return pos;
	};
	
	this.insertBox = function(box){
		WGC.data.box.push(box);
		var pos = WGC.data.box.length -1;
		WGC.data.box[pos].boxId = pos;
		return pos;
	};
	
	this.insertEntityState = function(entityState){
		WGC.data.entitystate.push(entityState);
		var pos = WGC.data.entitystate.length -1;
		WGC.data.entitystate[pos].entitystateId = pos;
		return pos;
	};
	
	this.insertEntity = function(entity){
		WGC.data.entity.push(entity);
		var pos = WGC.data.entity.length -1;
		WGC.data.entity[pos].entityId = pos;
		return pos;
	};
	
	this.insertGameEntity = function(gameEntity){
		WGC.data.gameentity[0] = gameEntity; //TODO: allow more entities
		var pos = 0; //TODO: allow more entities
		//WGC.data.gameentity.push(gameEntity);
		//var pos = WGC.data.gameentity.length -1;
		WGC.data.gameentity[pos].gameEntityId = pos;
		return pos;
	};
	
	this.removeGameEntity = function(gameEntity){
		WGC.data.gameentity[gameEntity.gameEntityId] = null;
	};
	
	this.insertGameBox = function(gameBox){
		WGC.removeGameBox(gameBox);
		
		WGC.data.gamebox.push(gameBox);
		var pos = WGC.data.gamebox.length -1;
		WGC.data.gamebox[pos].gameBoxId = pos;
		return pos;
	};
	
	this.removeGameBox = function(gameBox){
		for(var i = 0; i < WGC.data.gamebox.length; i++){
			var curr = WGC.data.gamebox[i];
			if(curr && curr.posX == gameBox.posX && curr.posY == gameBox.posY && curr.gameBoxLayerId == gameBox.gameBoxLayerId){
				WGC.data.gamebox[i] = null;
				for(var j = 0; j < WGC.data.gameboxstate.length; j++){
					if(WGC.data.gameboxstate[j] && WGC.data.gameboxstate[j].gameBoxId == i){
						WGC.data.gameboxstate[j] = null;
					}
				}
			}
		}
	};
	
	this.insertGameBoxState = function(gameBoxState){
		WGC.data.gameboxstate.push(gameBoxState);
		var pos = WGC.data.gameboxstate.length -1;
		WGC.data.gameboxstate[pos].gameBoxStateId = pos;
		return pos;
	};
	
	
	this.getDate = function(){
		var date = new Date();
		var year = date.getFullYear();
		var month = (date.getMonth() < 9)? "0" + (date.getMonth()+1) : (date.getMonth()+1);
		var day = (date.getDate() < 10)? "0" + date.getDate(): date.getDate();
		
		var hours = (date.getHours() < 10)? "0" + date.getHours(): date.getHours();
		var minutes = (date.getMinutes() < 10)? "0" + date.getMinutes(): date.getMinutes();
		var seconds = (date.getSeconds() < 10)? "0" + date.getSeconds(): date.getSeconds();
		
		return year + "-" + month + "-" + day + "T" + hours + ":" + minutes + ":" + seconds;
	};

};